package cz.muni.fi.pa165.pokemon.service.impl;

import cz.muni.fi.pa165.pokemon.dao.StadiumDAO;
import cz.muni.fi.pa165.pokemon.dto.BadgeTO;
import cz.muni.fi.pa165.pokemon.dto.StadiumTO;
import cz.muni.fi.pa165.pokemon.entity.Badge;
import cz.muni.fi.pa165.pokemon.entity.Stadium;
import cz.muni.fi.pa165.pokemon.service.StadiumService;
import java.util.ArrayList;
import java.util.List;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Peter Mravec
 */
@Service
public class StadiumServiceImpl implements StadiumService{
    
    @Autowired
    private StadiumDAO stadiumDAO;
    
    @Autowired
    private DozerBeanMapper mapper;

    @Transactional
    public void create(StadiumTO stadium) {
        if (stadium == null) {
            throw new IllegalArgumentException("stadium is null");
        }
        if (stadium.getId() != null) {
            throw new IllegalArgumentException("stadium.id is not null");
        }
        Stadium ord = mapper.map(stadium, Stadium.class);
        stadiumDAO.create(ord);
        stadium.setId(ord.getId());
    }

    @Transactional(readOnly=true)
    public StadiumTO get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
        return mapper.map(stadiumDAO.get(id), StadiumTO.class);
    }

    @Transactional
    public void update(StadiumTO stadium) {
        if (stadium == null) {
            throw new IllegalArgumentException("stadium is null");
        }
        if (stadium.getId() == null) {
            throw new IllegalArgumentException("stadium.id is null");
        }
        Stadium ord = mapper.map(stadium, Stadium.class);
        stadiumDAO.update(ord);
    }

    @Transactional
    public void remove(StadiumTO stadium) {
        if (stadium == null) {
            throw new IllegalArgumentException("stadium is null");
        }
        if (stadium.getId() == null) {
            throw new IllegalArgumentException("stadium.id is null");
        }
        Stadium ord = mapper.map(stadium, Stadium.class);
        stadiumDAO.remove(ord);
    }

    @Transactional(readOnly=true)
    public List<StadiumTO> findAll() {
        List<Stadium> result = stadiumDAO.findAll();
        List<StadiumTO> ret = new ArrayList<>();
        for (Stadium ord : result) {
            ret.add(mapper.map(ord, StadiumTO.class));
        }
        return ret;
    }

    @Transactional(readOnly=true)
    public List<StadiumTO> findByBadge(BadgeTO badge) {
        if (badge == null) {
            throw new IllegalArgumentException("badge is null");
        }
        List<Stadium> result = stadiumDAO.findByBadge(mapper.map(badge, Badge.class));
        List<StadiumTO> ret = new ArrayList<>();
        for (Stadium ord : result) {
            ret.add(mapper.map(ord, StadiumTO.class));
        }
        return ret;
    }
    
}